package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
)

var staticHandler http.Handler

func runHttp() {
	fmt.Println("listen at " + conf.Host)
	staticHandler = http.StripPrefix("/assets/", http.FileServer(http.Dir(conf.OutPath)))
	http.HandleFunc("/assets/", StaticServer)
	err := http.ListenAndServe(conf.Host, staticHandler)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

// 静态文件处理
func StaticServer(w http.ResponseWriter, req *http.Request) {
	fmt.Println("path:" + req.URL.Path)
	staticHandler.ServeHTTP(w, req)
}

func my_http(w http.ResponseWriter, r *http.Request) {
	if "POST" == r.Method {
		r.ParseMultipartForm(8 << 20)
		file, _, err := r.FormFile("apk")
		if err != nil {
			http.Error(w, err.Error(), 500)
			return
		}
		defer file.Close()
		f, err := os.Create("filenametosaveas")
		defer f.Close()

		io.Copy(f, file)
		return
	}
	// 上传页面
	w.Header().Add("Content-Type", "text/html")
	w.WriteHeader(200)
	html := `
<form enctype="multipart/form-data" action="#" method="POST">
    Send this file: <input name="apk" type="file" />
    <input type="submit" value="Send File" />
</form>
`
	io.WriteString(w, html)
}
